package com.ai.daofa; /*
 * @author: changchun.liang
 * @date: 2024/4/6
 * @Email: liangcc0309@dingtalk.com
 */


import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import lombok.Data;

@Data
public class Yuan {
    private final String fix = "_";
    private final String fix2 = ";";
    private final String fix3 = ":";
    /**
     * 语句
     */
    private String token;
    /**
     * 维度1
     */
    private String w1;
    /**
     * 维度2
     */
    private String w2;
    /**
     * 维度3
     */
    private String w3;
    /**
     * 维度4
     */
    private String w4;
    /**
     * 维度5
     */
    private String w5;
    /*** 维度6
     */
    private String w6;
    /*** 维度7
     */
    private String w7;
    /*** 维度8
     */
    private String w8;
    /**
     * 关键值1
     */
    private String k1;
    /**
     * 关键值2
     */
    private String k2;
    /**
     * 关键值3
     */
    private String k3;
    /**
     * 关键值4
     */
    private String k4;
    /**
     * 关键值5
     */
    private String k5;
    /*** 关键值6
     */
    private String k6;
    /*** 关键值7
     */
    private String k7;
    /*** 形容词
     */
    private String xCi;
    /*** 动词
     */
    private String dCi;
    /*** 副词
     */
    private String fCi;
    /*** 名次
     */
    private String mCi;
    /**主语*/
    private String zhu;
    /**谓语*/
    private String wei;
    /**宾语*/
    private String bing;
    /**
     * 人
     */
    private String ren;
    /**
     * 事
     */
    private String shi;
    /**
     * 物
     */
    private String wu;
    /**
     * 语义
     */
    private String ming;
    /**
     * 词库分词文件地址
     */
    private String ciKuPath = super.getClass().getName() + "ciku.txt";

    private List<String> cikuList = new ArrayList<String>();
    private Map<String,String> cikuMap = new HashMap<String,String>();

    public void initKeyWord(){
        // 创建File对象
        //File file = new File(ciKuPath);

        // 如果文件不存在，就创建它
        if (!FileUtil.exist(ciKuPath)) {
            /*
            try {
                boolean fileCreated = file.createNewFile();
                if (fileCreated) {
                    System.out.println("文件创建成功：" + ciKuPath);
                } else {
                    System.out.println("文件创建失败：" + ciKuPath);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("文件已存在：" + ciKuPath);
            */
        } else {
            FileReader fileReader = new FileReader(ciKuPath);
            String result = fileReader.readString();
            if (null != result && !"".equals(result)) {
                String[] keyWord = result.split(fix);
                for (String word : keyWord) {
                    if (null != word && !"".equals(word)) {
                        cikuMap.put(word, "");
                    }
                }
                for (Map.Entry entry : cikuMap.entrySet()) {
                    if (null != entry && null != entry.getKey()) {
                        String key = String.valueOf(entry.getKey());
                        cikuList.add(key);
                    }
                }
            }
        }
    }

    public void saveKeyWord(char word) throws IOException {
        saveKeyWord(String.valueOf(word));
    }

    public void saveKeyWord(String word) throws IOException {
        if (!cikuMap.containsKey(word)) {
            cikuMap.put(word,"");
            cikuList.add(word);
            String result = "";
            for(Map.Entry entry: cikuMap.entrySet()){
                if (null!=entry && null!=entry.getKey()) {
                    result = result +fix+ String.valueOf(entry.getKey());
                }
            }
            FileWriter writer = new FileWriter(ciKuPath);
            writer.write(result);
            System.out.println("保存 "+result);
        }   else {
            System.out.println("关键字已存在,不保存 "+word);
        }
    }

    public void saveCiLei(String word) throws IOException {
        FileWriter writer = new FileWriter(ciLeiPath);
        writer.write(word+"\n",true);
        System.out.println("保存词分类:"+word);
    }

    /**
     * 保存绑定关系
     * @param path
     * @param word
     * @throws IOException
     */
    public void saveBind(String path,String word) throws IOException {
        FileWriter writer = new FileWriter(path);
        writer.write(word+"\n",true);
        System.out.println("保存绑定类容:"+word+",路径:"+path);
    }

    /**
     * 关键词 绑定类型 含义
     * @param rs
     */
    public void  bindLeiXing(List<String[]> rs){
        //读取类型数组
        FileReader fileReader = new FileReader(ciLeiPath);
        List<String> leiXing = fileReader.readLines();
        if (leiXing!=null && leiXing.size()>0) {
            //循环判断
            rs.forEach(o -> {
                String wordRdn = Arrays.toString(o).replaceAll("[\\[\\], ]", "");
                for (String lx:leiXing){
                    System.out.println(wordRdn + " 是【"+lx+"】吗?");
                    Scanner scanner = new Scanner(System.in);
                    System.out.println("请输入文本框(按enter结束)");
                    String input = scanner.nextLine();
                    if ("Y".equals(input) || "是".equals(input)) {
                        try {
                            saveBind(ciLeiBindPath,wordRdn+fix2+lx);
                        } catch (IOException e) {
                            e.printStackTrace();
                            System.out.println("保存异常");
                        }
                    }
                }
            });
        } else {
            System.out.println("还没有词类型,不进行词类绑定");
        }
    }

    public void bindMing(){
        System.out.println(this.token + " 的意思是什么?");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入文本框(按enter结束)");
        String input = scanner.nextLine();
        try {
            if (input.contains("意思是") || input.contains("代指")|| input.contains("释义")) {
                saveBind(tokenMingPath, this.token + fix3 + input);
            }
       } catch (IOException e) {
                e.printStackTrace();
                System.out.println("保存异常");
        } finally {
            if ("1".equals(input) || "N".equals(input)|| "n".equals(input) || "否".equals(input)|| "没有了".equals(input)){
            } else {
                bindMing();
            }
        }
    }

    /**
     * 根据语义判断是否创建内容
     * @param path
     * @param word
     * @throws IOException
     */
    public void saveMemory(String path,String word) throws IOException {
            FileWriter writer = new FileWriter(path);
            writer.write(word);
            System.out.println("自动创建:"+path+"保存:"+word);
    }

    /**
     * 词库数据地址
     */
    private String yuanPath = super.getClass().getName() + "yuan.txt";

    /**
     * 词库类型分类地址
     */
    private String ciLeiPath = super.getClass().getName() + "ciLei.txt";
    /**
     * 关键词 与词类 绑定关系
     */
    private String ciLeiBindPath = super.getClass().getName() + "ciLeiBind.txt";

    /**
     * token的释义
     */
    private String tokenMingPath = super.getClass().getName() + "tokenMing.txt";

    /**
     * 临时地址
     */
    private String tmpPath = super.getClass().getName() + "temp.txt";

    public void savePractice(String word) throws IOException {
        FileWriter writer = new FileWriter(tmpPath);
        writer.write(word);
        System.out.println("保存"+word);

    }


}
